package com.schd.base.controller;

import com.schd.base.service.BaseService;
import com.schd.base.utils.BeanUtil;
import com.schd.base.vo.DataTransferObject;
import com.schd.base.vo.PageableVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author 陈刚
 * @version 1.0
 * @project ZNSM
 * @package com.haorui.base.core.controller
 * @createDate 2017-08-18 18:01
 * @modfiyDate
 * @function controller 基础类
 */
public class BaseController<Entity, DTO extends DataTransferObject<Entity>> {

    private BaseService<Entity,String> baseService;

    public BaseController(BaseService<Entity, String> baseService) {
        this.baseService = baseService;
    }

    @ApiOperation(value = "新增一个对象", notes = "返回新增实体对象", position = 2)
    @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Entity createEntity(@RequestBody DTO dto) {
        return baseService.save(dto.buildPO());
    }

    @ApiOperation(value = "删除对象", notes = "返回状态码", position = 2)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteEntity(@PathVariable(value = "id") String id) {
        try{
            baseService.delete(id);
            return ResponseEntity.ok("删除成功");
        }catch (Exception e){
            return  ResponseEntity.status(405).body("无法删除的数据");
        }


    }

    @ApiOperation(value = "删除对象", notes = "返回状态码", position = 2)
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public ResponseEntity deleteBatchEntity(@RequestBody String[] ids) {
        try{
            baseService.delete(ids);
            return ResponseEntity.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(405).body("无法删除的数据");
        }
    }

    @ApiOperation(value = "修改对象所有属性", notes = "返回修改后的对象", position = 2)
    @RequestMapping(value = "/update", method = RequestMethod.PUT,consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Entity updateEntity(@RequestBody DTO dto) {
        if(dto.getId()==null){
            return dto.buildPO();
        }else{
            return baseService.save(dto.buildPO());
        }

    }

    @ApiOperation(value = "修改对象部分属性", notes = "返回修改后的对象", position = 2)
    @RequestMapping(value = "/update", method = RequestMethod.PATCH,consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Entity updatePATCHEntity(@RequestBody DTO dto) {
        if (dto.getId() == null) {
            return dto.buildPO();
        }

        Entity dbEnetity = baseService.findOne(dto.getId());
        BeanUtil.beanCopyNotNull(dto, dbEnetity);
        return  baseService.save(dbEnetity);
    }

    @ApiOperation(value = "按ID查询对象", notes = "返回查询对象", position = 2)
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Entity getEntityById(@PathVariable String id) {
        return baseService.findOne(id);
    }

    @ApiOperation(value = "获取所有实体对象", notes = "返回实体列表", position = 2)
    @RequestMapping(value = "/alllist", method = RequestMethod.GET)
    public List<Entity> findAll() {
        return baseService.findAll();
    }

    @ApiOperation(value = "分页查询(可带查询条件)", notes = "返回实体列表", position = 2)
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public Page<Entity> findbyPage(@RequestBody PageableVo pageableVo) {
        return baseService.findAll(pageableVo.getPageModel().pageRequest(),pageableVo.getSearchConditions());
    }
}
